import { getResumeList, getResumeDetail, createResume, updateResume, deleteResume } from "@/api/resume";
import { ElMessage } from "element-plus";

const state = {
    resumeList: [],
    currentResume: null,
    loading: false,
    pagination: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
    },
};

const getters = {
    resumeCount: (state) => state.resumeList.length,
    hasResume: (state) => state.resumeList.length > 0,
};

const mutations = {
    SET_RESUME_LIST(state, { list, total }) {
        state.resumeList = list;
        state.pagination.total = total || list.length;
    },
    SET_CURRENT_RESUME(state, resume) {
        state.currentResume = resume;
    },
    SET_LOADING(state, loading) {
        state.loading = loading;
    },
    SET_PAGINATION(state, pagination) {
        state.pagination = { ...state.pagination, ...pagination };
    },
    ADD_RESUME(state, resume) {
        state.resumeList.unshift(resume);
        state.pagination.total += 1;
    },
    UPDATE_RESUME(state, resume) {
        const index = state.resumeList.findIndex((item) => item.id === resume.id);
        if (index !== -1) {
            state.resumeList.splice(index, 1, resume);
        }
        if (state.currentResume && state.currentResume.id === resume.id) {
            state.currentResume = resume;
        }
    },
    REMOVE_RESUME(state, id) {
        state.resumeList = state.resumeList.filter((resume) => resume.id !== id);
        state.pagination.total -= 1;
        if (state.currentResume && state.currentResume.id === id) {
            state.currentResume = null;
        }
    },
};

const actions = {
    // 获取简历列表
    async getResumeList({ commit }, params) {
        commit("SET_LOADING", true);
        try {
            const response = await getResumeList(params);
            if (response.code === 200 || response.code === 0) {
                const { list, total, current_page, page_size } = response.data;
                commit("SET_RESUME_LIST", { list, total });
                commit("SET_PAGINATION", { currentPage: current_page, pageSize: page_size });
                return Promise.resolve(response.data);
            } else {
                return Promise.reject(response);
            }
        } catch (error) {
            return Promise.reject(error);
        } finally {
            commit("SET_LOADING", false);
        }
    },

    // 获取简历详情
    async getResumeDetail({ commit }, id) {
        commit("SET_LOADING", true);
        try {
            const response = await getResumeDetail(id);
            if (response.code === 200 || response.code === 0) {
                commit("SET_CURRENT_RESUME", response.data);
                return Promise.resolve(response.data);
            } else {
                return Promise.reject(response);
            }
        } catch (error) {
            return Promise.reject(error);
        } finally {
            commit("SET_LOADING", false);
        }
    },

    // 创建简历
    async createResume({ commit }, data) {
        commit("SET_LOADING", true);
        try {
            const response = await createResume(data);
            if (response.code === 200 || response.code === 0) {
                commit("ADD_RESUME", response.data);
                ElMessage({
                    type: "success",
                    message: "简历创建成功",
                });
                return Promise.resolve(response.data);
            } else {
                return Promise.reject(response);
            }
        } catch (error) {
            return Promise.reject(error);
        } finally {
            commit("SET_LOADING", false);
        }
    },

    // 更新简历
    async updateResume({ commit }, data) {
        commit("SET_LOADING", true);
        try {
            const response = await updateResume(data);
            if (response.code === 200 || response.code === 0) {
                commit("UPDATE_RESUME", response.data);
                ElMessage({
                    type: "success",
                    message: "简历更新成功",
                });
                return Promise.resolve(response.data);
            } else {
                return Promise.reject(response);
            }
        } catch (error) {
            return Promise.reject(error);
        } finally {
            commit("SET_LOADING", false);
        }
    },

    // 删除简历
    async deleteResume({ commit }, id) {
        commit("SET_LOADING", true);
        try {
            const response = await deleteResume(id);
            if (response.code === 200 || response.code === 0) {
                commit("REMOVE_RESUME", id);
                ElMessage({
                    type: "success",
                    message: "简历删除成功",
                });
                return Promise.resolve(response.data);
            } else {
                return Promise.reject(response);
            }
        } catch (error) {
            return Promise.reject(error);
        } finally {
            commit("SET_LOADING", false);
        }
    },
};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions,
};
